package com.servlettest.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GetServerInfoServlet extends HttpServlet {

	private static final long serialVersionUID = 1523741378824582689L;
	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		printContent(request,out);
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}
	public void printContent(HttpServletRequest request, PrintWriter out) {
		out.println("<H2>SERVER INFO<br>");
		out.println("<table border='1'>");
		out.println("<tr><td>server name:</td><td>"+request.getServerName()+"</td></tr>");
		out.println("<tr><td>server ip:</td><td>"+request.getRemoteAddr()+"</td></tr>");
		out.println("<tr><td>server ServerPort:</td><td>"+request.getServerPort()+"</td></tr>");
		out.println("<tr><td>server ContextPath:</td><td>"+request.getContextPath()+"</td></tr>");
		out.println("<tr><td>server Scheme:</td><td>"+request.getScheme()+"</td></tr>");
		out.println("<tr><td>server Method:</td><td>"+request.getMethod()+"</td></tr>");
		out.println("<tr><td>server RequestURI:</td><td>"+request.getRequestURI()+"</td></tr>");
		out.println("<tr><td>server ServletPath:</td><td>"+request.getServletPath()+"</td></tr>");
		out.println("</table>");
	}
	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
